package edu.ptu.acl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ptu.acl.common.AdminValidate;
import edu.ptu.acl.common.RequestHolder;
import edu.ptu.acl.common.ResultVO;
import edu.ptu.acl.dto.UserTransferDto;
import edu.ptu.acl.dto.param.IdListParam;
import edu.ptu.acl.entity.Dept;
import edu.ptu.acl.entity.RoleUser;
import edu.ptu.acl.entity.User;
import edu.ptu.acl.service.IDeptService;
import edu.ptu.acl.service.IRoleUserService;
import edu.ptu.acl.service.IUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LinJC
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/roleUser")
public class RoleUserController {

    @Resource
    private IRoleUserService roleUserService;
    @Resource
    private IUserService userService;
    @Resource
    private IDeptService deptService;

    //角色用户穿梭框结构获取
    @GetMapping("/getRoleUser/{roleId}")
    public ResultVO getRoleUser(@PathVariable Long roleId){
        HashMap<String, Object> result = new HashMap<>();

        // 获取所有用户
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","1");
        List<User> allUserList = userService.list(queryWrapper);
        // admin 直接给全部
        if (AdminValidate.isAdmin()) {
            result.put("allUserList",allUserList);
        }else {
            // 只能操控当前用户部门下的用户
            User currentUser = RequestHolder.getCurrentUser();
            // 查询部门
            Dept currentUserDept = deptService.getById(currentUser.getDeptId());
            // 递归查询部门及子级部门下的用户
            List<UserTransferDto> userTransferDtoList = new ArrayList<>();
            userService.recursionGetDeptUser(currentUserDept.getId(),userTransferDtoList);
            // 给这些用户的disabled设为false即可选状态
            List<UserTransferDto> allUserTransferDtoList = new ArrayList<>();
            for (User user : allUserList) {
                UserTransferDto userTransferDto = UserTransferDto.copyUser2Dto(user);
                if (userTransferDtoList.contains(userTransferDto)){
                    userTransferDto.setDisabled(false);
                }
                allUserTransferDtoList.add(userTransferDto);
            }
            result.put("allUserList",allUserTransferDtoList);
        }

        // 获取该角色拥有的用户
        QueryWrapper<RoleUser> roleUserQueryWrapper = new QueryWrapper<>();
        roleUserQueryWrapper.eq("role_id",roleId);
        List<RoleUser> roleUserList = roleUserService.list(roleUserQueryWrapper);
        Set<String> userIds = roleUserList.stream().map((user)-> user.getUserId().toString())
                .collect(Collectors.toSet());
        Set<String> checkUserIdList = new HashSet<>();
        if (CollectionUtils.isNotEmpty(userIds)){
            // 去除冻结用户
            List<User> users = userService.listByIds(userIds);
            checkUserIdList = users.stream().filter(user -> StringUtils.equals(user.getStatus(), "1"))
                    .map(user -> user.getId().toString()).collect(Collectors.toSet());
        }

        result.put("checkedUsers",checkUserIdList);
        return ResultVO.success(result);
    }
    //
    //编辑角色用户关系
    @PutMapping("/updateRoleUser/{roleId}")
    public ResultVO updateRoleUser(@PathVariable Long roleId, @RequestBody IdListParam idListParam){

        roleUserService.updateRoleUser(roleId, Arrays.asList(idListParam.getIds()));
        return ResultVO.success();
    }

}
